package com.easyhouse.service;


import com.easyhouse.common.pojo.Product;
import com.easyhouse.common.utils.MapperUtil;
import com.easyhouse.common.utils.UUIDUtil;
import com.easyhouse.common.vo.EasyUIResult;
import com.easyhouse.common.vo.SysResult;
import com.easyhouse.mapper.ProductMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class ProductService {
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private SearchFeign searchFeign;
    @Autowired
    private StringRedisTemplate redisTemplate;
    public EasyUIResult queryByPage(Integer page, Integer rows) {
        int index = (page-1)*rows;
        int end = page*rows;
        List<Product> products = productMapper.selectProduct(index,end);
        int count  = productMapper.getCount();
        EasyUIResult easyUIResult = new EasyUIResult(count,products);
        return easyUIResult;
    }


    public SysResult saveProduct(Product product) {
        try{
            //生成产品id
            String houseId = UUIDUtil.getUUID();
            product.setHouseId(houseId);
            /*String userId = "admin";
            product.setUserId(userId);*/
            productMapper.saveProduct(product);
            //设置redis键值对
            String key ="house_"+houseId;
            String hJson = MapperUtil.MP.writeValueAsString(product);
            //存储到redis
            redisTemplate.opsForValue().set(key,hJson,2, TimeUnit.DAYS);
            //转为字符串
            String productString = product.toString();
            //添加索引数据
            searchFeign.addingIndexes(productString);
            return SysResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return SysResult.build(201,"新增失败",null);
        }
    }

    public void editProduct(Product product) {
        String key = "house_"+product.getHouseId();
        try {
            redisTemplate.opsForValue().setIfPresent(key,
                    MapperUtil.MP.writeValueAsString(product),
                    2,TimeUnit.DAYS);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        productMapper.editProduct(product);
    }

    public void deleteProduct(String ids) {
        String key = "house_"+ids;
        redisTemplate.delete(key);
        productMapper.deleteProductById(ids);
    }

    public Product queryByProductId(String houseId) {
        String key = "house_" + houseId;
        if (redisTemplate.hasKey(key)) {
            String hJson = redisTemplate.opsForValue().get(key);
            try {
                return MapperUtil.MP.readValue(hJson, Product.class);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
                return null;
            }
        } else {
                Product product = productMapper.seelctProductById(houseId);
                try {
                    String hJson = MapperUtil.MP.writeValueAsString(product);
                    redisTemplate.opsForValue().set(key, hJson, 2, TimeUnit.DAYS);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    return null;
                }
                return product;
         }
    }

    public List<Product> selectSeckillProduct() {
        return productMapper.selectSeckillProduct();
    }

    public EasyUIResult queryByPageget(Integer page, Integer rows) {
        int index = (page-1)*rows;
        int end = page*rows;
        List<Product> products = productMapper.selectProduct2(index,end);
        int count  = productMapper.getCount2();
        EasyUIResult easyUIResult = new EasyUIResult(count,products);
        return easyUIResult;
    }
}
